package com.lianbang.pharmacy.module.user

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import cn.ubest.mvvmcore.base.BaseActivity
import cn.ubest.mvvmcore.utils.SpanUtils
import com.google.android.material.tabs.TabLayout
import com.lianbang.pharmacy.R
import com.lianbang.pharmacy.databinding.ActivityOrderBinding
import com.lianbang.pharmacy.module.medicine.detail.MedicineDetailInfoFragment
import com.lianbang.pharmacy.vm.MainVM
import kotlinx.android.synthetic.main.activity_medicine_detail.*

/**
 * Created by maoqi on 2020/12/17.
 */
class OrderActivity : BaseActivity<MainVM, ActivityOrderBinding>() {

    companion object {
        fun start(context: Context?, pagePosition: Int) {
            context?.also {
                val starter = Intent(it, OrderActivity::class.java)
                if (it !is Activity) {
                    starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                starter.putExtra("pagePosition", pagePosition)
                it.startActivity(starter)
            }
        }
    }

    override fun initLayout(): Int {
        return R.layout.activity_order
    }

    override fun initData(intent: Intent) {

    }

    override fun initView() {
        val titleList = mutableListOf("全部", "已下订单", "已发货订单", "已完成订单")
        val fragmentList = mutableListOf(
            OrderListFragment.newInstance(),
            OrderListFragment.newInstance(),
            OrderListFragment.newInstance(),
            OrderListFragment.newInstance()
        )
        tl_tab.setupWithViewPager(vp_pager)
        tl_tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                val text = tab.text.toString().trim()
                tab.text = SpanUtils().append(text).setFontSize(16,true).setBold().create()
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                val text = tab.text.toString().trim()
                tab.text = SpanUtils().append(text).setFontSize(12,true).create()
            }

            override fun onTabReselected(tab: TabLayout.Tab) {}
        })
        vp_pager.adapter = object : FragmentStatePagerAdapter(
            supportFragmentManager,
            FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getPageTitle(position: Int): CharSequence? {
                return titleList[position]
            }

            override fun getItem(position: Int): Fragment {
                return fragmentList[position]
            }

            override fun getCount(): Int {
                return fragmentList.size
            }
        }
        val pagePosition = intent.getIntExtra("pagePosition", 0)
        vp_pager.currentItem = pagePosition
    }

    override fun loadData() {

    }

    override fun setToolbarTitle(): String? {
        return "我的订单"
    }

}